<template>
  <div class="layout-padding">
    <div class="layout-padding-auto layout-padding-view">
      <el-row>
        <div class="mb8" style="width: 100%">
          <el-button formDialogRef icon="folder-add" type="primary" class="ml10" @click="formDialogRef.openDialog()"
            v-auth="'coalSystem-biz_CoalUserInfo_add'">
            {{ $t('common.addBtn') }}
          </el-button>
          <el-button plain formDialogRef :disabled="multiple" icon="Delete" type="primary" class="ml10"
            v-auth="'coalSystem-biz_CoalUserInfo_del'" @click="handleDelete(selectObjs)">
            {{ $t('common.delBtn') }}
          </el-button>
          <right-toolbar v-model:showSearch="showSearch" 	:export="'coalSystem-biz_CoalUserInfo_export'"
						@exportExcel="exportExcel" class="ml10" style="float: right;margin-right: 20px"
            @queryTable="getDataList"></right-toolbar>
        </div>
      </el-row>
      <el-table :data="state.dataList" v-loading="state.loading" style="width: 100%"
        @selection-change="handleSelectionChange" @sort-change="sortChangeHandle">
        <el-table-column type="selection" width="40" align="center" />
        <el-table-column type="index" :label="t('CoalUserInfo.index')" width="40" />
          <el-table-column prop="openid" :label="t('CoalUserInfo.openid')"  show-overflow-tooltip/>
          <el-table-column prop="nickname" :label="t('CoalUserInfo.nickname')"  show-overflow-tooltip/>
          <el-table-column prop="fullName" :label="t('CoalUserInfo.fullName')"  show-overflow-tooltip/>
          <el-table-column prop="phoneNumber" :label="t('CoalUserInfo.phoneNumber')"  show-overflow-tooltip/>
          <el-table-column prop="region" :label="t('CoalUserInfo.region')"  show-overflow-tooltip/>
          <el-table-column prop="detailedAddress" :label="t('CoalUserInfo.detailedAddress')"  show-overflow-tooltip/>
          <el-table-column prop="coordinate" :label="t('CoalUserInfo.coordinate')"  show-overflow-tooltip/>
          <el-table-column prop="avatarAddress" :label="t('CoalUserInfo.avatarAddress')"  show-overflow-tooltip/>
          <el-table-column prop="userType" :label="t('CoalUserInfo.userType')"  show-overflow-tooltip/>
          <el-table-column prop="createDate" :label="t('CoalUserInfo.createDate')"  show-overflow-tooltip/>
          <el-table-column prop="sort" :label="t('CoalUserInfo.sort')"  show-overflow-tooltip/>
          <el-table-column prop="loginAccount" :label="t('CoalUserInfo.loginAccount')"  show-overflow-tooltip/>
          <el-table-column prop="password" :label="t('CoalUserInfo.password')"  show-overflow-tooltip/>
        <el-table-column :label="$t('common.action')" width="150">
          <template #default="scope">
            <el-button icon="edit-pen" text type="primary" v-auth="'coalSystem-biz_CoalUserInfo_edit'"
              @click="formDialogRef.openDialog(scope.row.userInfoId)">{{ $t('common.editBtn') }}</el-button>
            <el-button icon="delete" text type="primary" v-auth="'coalSystem-biz_CoalUserInfo_del'" @click="handleDelete([scope.row.userInfoId])">{{
              $t('common.delBtn')
            }}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" v-bind="state.pagination" />
    </div>

    <!-- 编辑、新增  -->
    <form-dialog ref="formDialogRef" @refresh="getDataList(false)" />
  </div>
</template>

<script setup lang="ts" name="systemCoalUserInfo">
import { BasicTableProps, useTable } from "/@/hooks/table";
import { fetchList, delObjs } from "/@/api/coalSystem-biz/CoalUserInfo";
import { useMessage, useMessageBox } from "/@/hooks/message";
import { useDict } from '/@/hooks/dict';
import { useI18n } from "vue-i18n";

// 引入组件
const FormDialog = defineAsyncComponent(() => import('./form.vue'));
const { t } = useI18n()
// 定义查询字典

// 定义变量内容
const formDialogRef = ref()
// 搜索变量
const queryRef = ref()
const showSearch = ref(true)
// 多选变量
const selectObjs = ref([]) as any
const multiple = ref(true)

const state: BasicTableProps = reactive<BasicTableProps>({
  queryForm: {},
  pageList: fetchList
})

//  table hook
const {
  getDataList,
  currentChangeHandle,
  sizeChangeHandle,
  sortChangeHandle,
  downBlobFile
} = useTable(state)

// 清空搜索条件
const resetQuery = () => {
  // 清空搜索条件
  queryRef.value?.resetFields()
  // 清空多选
  selectObjs.value = []
  getDataList()
}

// 导出excel
const exportExcel = () => {
  downBlobFile('/coalSystem-biz/CoalUserInfo/export', state.queryForm, 'CoalUserInfo.xlsx')
}

// 多选事件
const handleSelectionChange = (objs: any) => {
	selectObjs.value.push(...objs.map((val: any) => val.userInfoId));
	multiple.value = !objs.length;
};

// 删除操作
const handleDelete = async (ids: string[]) => {
	try {
		await useMessageBox().confirm(t('common.delConfirmText'));
	} catch {
		return;
	}

	try {
		await delObj(ids);
		getDataList();
		useMessage().success(t('common.delSuccessText'));
	} catch (err: any) {
		useMessage().error(err.msg);
	}
};
</script>